package com.ityuxin.web;

import com.ityuxin.common.Result;
import com.ityuxin.domain.ServiceItemlist;
import com.ityuxin.domain.User;
import com.ityuxin.service.ServiceitemService;
import com.ityuxin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.awt.*;
import java.util.List;

/**
 * @Author: yyh
 * @Description:
 * @Date:Create：in 2021/4/3 11:52
 * @Modified By：
 */
@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    private UserService userService;

    /**
     * @Author:
     * @Description:查询全部
     * @Date: 12:27 2021/4/3
     * @param: model
     * @return: java.lang.String
     * @Version: 1.0
     */
    @RequestMapping("/findAll")
    public String findAll(Model model){
        System.out.println("findAll方法执行了...");
        List<User> list = userService.findAll();
        model.addAttribute("list",list);
        // 请求转发到list.jsp
        return "list";
    }


    /**
     * @Author:
     * @Description:根据id删除
     * @Date: 12:28 2021/4/3
     * @param: id
     * @return: java.lang.String
     * @Version: 1.0
     */
    @RequestMapping("/deleteById")
    public String deleteById(Integer id){
        userService.deleteById(id);
        // 重定向查询所有账号信息
        return "redirect:/account/findAll";
    }


    /**
     * @Author:
     * @Description:添加
     * @Date: 12:28 2021/4/3
     * @param: accountService
     * @return: java.lang.String
     * @Version: 1.0
     */
    @RequestMapping("/addserviceitem")
    public  String  addserviceitem(User user){
        userService.addserviceitem(user);
        //重定向
        return "redirect:/account/findAll";
    }


    /**
     * 根据id查询账户信息
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public String findById(Integer id,Model model){
        // 根据id查询数据信息
        User user = userService.findById(id);
        // 设置共享的数据信息
        model.addAttribute("user" ,user);
        // 请求转发到修改账户信息页面
        return "update";
    }

    /**
     * 修改账户
     * @param
     * @return
     */
    @RequestMapping("/updateAccount")
    public String updateserviceitem(User user){
        // 调用service层修改
        userService.updateserviceitem(user);
        // 重定向查询所有数据信息
        return "redirect:/account/findAll";
    }


    @PostMapping("/login")
    public Result login(@RequestBody  User user) {
        //根据用户名查询密码
        String password = userService.login(user);
        try {
            if (user.getPassword().equals(password)) {
                return new Result(true, "ok", user);
            } else {
                return new Result(false, "账户或密码不正确");
            }
        }catch (Exception ex){
            return new Result(false, "账户或密码不正确");
        }
    }

}
